/****************
*PID:
*Auth:Jonariguez
*****************
*/
#define lson k*2,l,m
#define rson k*2+1,m+1,r
#define rep(i,s,e) for(i=(s);i<=(e);i++)
#define For(j,s,e) For(j=(s);j<(e);j++)
#define sc(x) scanf("%d",&x)
#define In(x) scanf("%I64d",&x)
#define SS(x,y) scanf("d",&x,&y)
#define pf(x) printf("%d",x)
#define pfn(x) printf("%d\n",(x))
#define Pf(x) printf("%I64d",(x))
#define Pfn(x) printf("%I64d\n",(x))
#define Pc printf(" ")
#define PY puts("YES")
#define PN puts("NO")
#define MP make_pair
#define X first
#define Y second
#define PI acos(-1.0)
#include <stdio.h>
#include <string.h>
#include <string>
#include <math.h>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef int Ll;
Ll quick_pow(Ll a,Ll b,Ll MOD){a%=MOD;Ll res=1;while(b){if(b&1)res=(res*a)%MOD;b/=2;a=(a*a)%MOD;}return res;}

const int maxn=200000+10;
const int MOD=1e9+7;

int Size[maxn],d[maxn];
LL sum[maxn],dp[maxn];
struct node{
    int to,w;
};

vector<node> vec[maxn];

LL res=0;

void dfs(int u,int pa){
    sum[u]=0;
    dp[u]=0;
    Size[u]=1;
    for(int i=0;i<vec[u].size();i++){
        int v=vec[u][i].to,w=vec[u][i].w;
        if(v==pa) continue;
        dfs(v,u);

        sum[v]+=(1LL*Size[v]*w)%MOD;
        sum[v]%=MOD;
        dp[u]+=(sum[v]*Size[u])%MOD;
        dp[u]%=MOD;
        dp[u]+=(sum[u]*Size[v])%MOD;
        //dp[u]+=1LL*Size[u]*w;
        dp[u]%=MOD;

        dp[u]+=dp[v];
        dp[u]%=MOD;
        sum[u]+=sum[v];
        sum[u]%=MOD;
        Size[u]+=Size[v];
    }
    res+=dp[u];
    res%=MOD;
}

int main()
{
    int i,j,n,T;
    int kcase=1;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        for(i=1;i<=n;i++)
            vec[i].clear();
        for(i=1;i<n;i++){
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            vec[u].push_back((node){v,w});
            vec[v].push_back((node){u,w});
        }
        memset(dp,0,sizeof(dp));
        memset(Size,0,sizeof(Size));
        memset(sum,0,sizeof(sum));
        res=0;
        dfs(1,0);
        res%=MOD;
        if(res<0) res+=MOD;
        printf("Case %d: %I64d\n",kcase++,res);
    }
    return 0;
}


